package gb.demo.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gb.demo.common.R;
import gb.demo.entity.Label;
import gb.demo.entity.User;
import gb.demo.service.LabelService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.annotation.RequestScope;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@MapperScan("gb.demo.mapper")
public class LabelController {
    @Autowired
    private LabelService labelService;

    @GetMapping("/movie/lableManagePage")
    public String movieManagePage() {
        return "movie/labelManagement";
    }

    //    测试
    @RequestMapping("/labelTest")
    @ResponseBody
    public String labelTest() {
        QueryWrapper<Label> wrapper = new QueryWrapper<>();
        wrapper.gt("labelid", 1).lt("labelid", 20);
        List<Label> list = labelService.list(wrapper);
        list.stream().forEach(System.out::println);
        return JSON.toJSONString(list);
    }

    //    全部标签
    @RequestMapping("/label/allLabel")
    @ResponseBody
    public R alllLabel() {
//        List<Label> list=labelService.list();
        List<Label> list = labelService.findAllLabels();
        return R.success(list);
    }

    //    更新
    @RequestMapping("/label/updata")
    @ResponseBody
    public R lebelUpdata(Label label,HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null && user.getIsadmin() == 1) {
            QueryWrapper<Label> labelQueryWrapper=new QueryWrapper<>();
            labelQueryWrapper.eq("labelname",label.getLabelname());
            Label label1=labelService.getOne(labelQueryWrapper);
            if(label1!=null)
                return R.error("标签已被使用");
            labelService.updateById(label);
            return R.success("修改成功");
        } else return R.error("请登录管理员账号后重试");
    }

    //    删除
    @RequestMapping("/label/delete")
    @ResponseBody
    public R lebelDelete(String labelid,HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null && user.getIsadmin() == 1) {
            if (labelService.removeById(labelid))
                return R.success("修改成功");
            else return R.error("删除失败");
        } else return R.error("请登陆管理员账号后重试");
    }

    //    添加
    @RequestMapping("/label/insert")
    @ResponseBody
    public R lebelInsert(Label label, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null && user.getIsadmin() == 1) {
            QueryWrapper<Label> labelQueryWrapper=new QueryWrapper<>();
            labelQueryWrapper.eq("labelname",label.getLabelname());
            Label label1=labelService.getOne(labelQueryWrapper);
            if(label1!=null)
                return R.error("标签已被使用");
            labelService.save(label);
            return R.success("添加成功");
        } else return R.error("请登陆管理员账号后重试");
    }
}
